package net.coding.li709.materialrefresher.keys;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.data.DataContainer;
import org.spongepowered.api.data.DataHolder;
import org.spongepowered.api.data.manipulator.mutable.common.AbstractData;
import org.spongepowered.api.data.merge.MergeFunction;
import org.spongepowered.api.data.value.mutable.Value;

import java.util.Optional;

@Getter
@Setter
@ToString
public class FlanData extends AbstractData<FlanData, ImmutableFlanData> {

    /**
     * 是否生成器生成的，如果是，被玩家捡后就变成否
     */
    private boolean isSpawn;

    /**
     * 生成点名字
     */
    private String spawnName;

    public FlanData(boolean isSpawn,String spawnName) {
        this.isSpawn = isSpawn;
        this.spawnName = spawnName;
    }
    private Value<Boolean> createIsSpawn() {
        return Sponge.getRegistry().getValueFactory().createValue(FlanKeys.IS_SPAWN, false, this.isSpawn);
    }

    private Value<String> createSpawnName() {
        return Sponge.getRegistry().getValueFactory().createValue(FlanKeys.SPAWN_NAME, "", this.spawnName);
    }

    @Override
    protected void registerGettersAndSetters() {
        registerFieldGetter(FlanKeys.IS_SPAWN, this::isSpawn);
        registerFieldSetter(FlanKeys.IS_SPAWN, this::setSpawn);
        registerKeyValue(FlanKeys.IS_SPAWN,this::createIsSpawn );

        registerFieldGetter(FlanKeys.SPAWN_NAME, this::getSpawnName);
        registerFieldSetter(FlanKeys.SPAWN_NAME, this::setSpawnName);
        registerKeyValue(FlanKeys.SPAWN_NAME,this::createSpawnName );
    }

    @Override
    public Optional<FlanData> fill(DataHolder dataHolder, MergeFunction overlap) {
        return Optional.empty();
    }

    @Override
    public Optional<FlanData> from(DataContainer container) {
        if (!container.contains(FlanKeys.IS_SPAWN.getQuery())) {
            return Optional.empty();
        }
        if (!container.contains(FlanKeys.SPAWN_NAME.getQuery())) {
            return Optional.empty();
        }
        this.isSpawn =  container.getBoolean(FlanKeys.IS_SPAWN.getQuery()).get();
        this.spawnName =  container.getString(FlanKeys.SPAWN_NAME.getQuery()).get();
        return Optional.of(this);
    }

    @Override
    public FlanData copy() {
        return new FlanData(isSpawn,spawnName);
    }

    @Override
    public ImmutableFlanData asImmutable() {
        return new ImmutableFlanData(isSpawn,spawnName);
    }

    @Override
    public int getContentVersion() {
        return 1;
    }

    @Override
    public DataContainer toContainer() {
        DataContainer dataContainer = super.toContainer();
        return dataContainer
                .set(FlanKeys.IS_SPAWN,this.isSpawn)
                .set(FlanKeys.SPAWN_NAME,this.spawnName);
    }
}
